package date;

import javax.xml.crypto.Data;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.Locale;

/**
 * 本地日期类
 * <p>
 * 从Java 8开始，java.time包提供了新的日期和时间API，主要涉及的类型有：
 * 本地日期和时间：LocalDateTime，LocalDate，LocalTime；
 * 带时区的日期和时间：ZonedDateTime；
 * 时刻：Instant；
 * 时区：ZoneId，ZoneOffset；
 * 时间间隔：Duration
 * 以及替换SimpleDateFormat的格式化类DateTimeFormatter
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/12/21 11:11
 */
public class LocalDateDemo {
    public static void main(String[] args) {
        // localDateTime();
        // localTime();
        duration();
        // zonedDatetime();
        // dateTimeFormatter();
        // instant();
    }
    //  instant和Date
    private static void instant() {
        Instant now1 = Instant.now();
        System.out.println(now1.getEpochSecond());
        System.out.println(now1.toEpochMilli());
        Date now2 = Date.from(now1);
        System.out.println(now2);

        Date date = new Date();
        System.out.println(date.getTime());
        Instant date2 = date.toInstant();
        System.out.println(date2.getEpochSecond());
    }

    // dateTimeFormatter基本用法
    private static void dateTimeFormatter() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy MM dd HH:mm:ss", Locale.CHINESE);
        String format = dtf.format(now);
        System.out.println(format);


    }

    // ZonedDateTime,时区日期时间类
    private static void zonedDatetime() {
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);
        ZonedDateTime now1 = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("UTC+08:00"));
        System.out.println(now1);
        System.out.println(now2);
        System.out.println(now2.toLocalDateTime());
    }

    private static void duration() {
        LocalTime now = LocalTime.now();
        LocalTime t1 = now.plusHours(1).plusMinutes(1).plusSeconds(11);
        Duration d = Duration.between(now, t1);
        // 时间间隔
        System.out.println(d.toDays());
        System.out.println(d.toHours());
        System.out.println(d.toMinutes());
        System.out.println(d.toMillis());
    }

    // localTime基本用法
    private static void localTime() {
        LocalTime now = LocalTime.now();
        LocalTime t1 = now.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(t1);
    }

    // LocalDateTime基本用法
    private static void localDateTime() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        LocalDateTime dt = LocalDateTime.of(2022, 12, 21, 11, 23, 22);
        System.out.println(dt);
        // 加减操作,plusxxx,minusxxx
        LocalDateTime dt1 = dt.plusDays(-2).minusDays(1).plusHours(2).plusMinutes(11);
        System.out.println(dt1);
        // 设置时间,withxxx
        LocalDateTime dt2 = dt.withMonth(11).withDayOfMonth(30).withHour(11).withMinute(11);
        System.out.println(dt2);
        // with()方法
        LocalDateTime dt3 = dt.with(Month.FEBRUARY);
        System.out.println(dt3);
        // 时间大小比较isBefore,isAfter
        System.out.println(dt.isAfter(now));
    }
}
